﻿using System.Collections.Generic;
using System.Linq;
using Database;

namespace DoMain
{
    public class LoginDomain
    {
        readonly DatabaseInfo _dbInfo = new DatabaseInfo();

        /// <summary>
        /// 根据输入的用户名和密码进行登陆操作，并返回登陆结果
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <returns></returns>
        public string LogIn(string userName, string userPassword)
        {
            string sql = "SELECT t.PASSWORD from USERS t where t.NAME=\'" + userName + "\'";
            List<string> result = _dbInfo.ColumnDataQuery(sql, "PASSWORD");

            if (result.Count == 0)
            {
                return "用户名不存在，请检查。";
            }
            var passWord = result.First();
            var message = passWord == userPassword ? null : "账号或密码输入错误，请检查。";
            return message;
        }

        /// <summary>
        /// 根据是否登录成功，更新系统参数中的用户名
        /// </summary>
        /// <param name="success"></param>
        /// <param name="userName"></param>
        public void LogInDone(bool success, string userName = null)
        {
            DatabaseInfo.UserName = success ? userName : null;
        }
    }
}